package club.justwill.single.Controller;

import club.justwill.single.Service.ProjectsService;
import club.justwill.single.aspect.LoginRequired;
import club.justwill.single.assembler.ProjectAssemble;
import club.justwill.single.assembler.ProjectSupportAssemble;
import club.justwill.single.assembler.StickAssemble;
import club.justwill.single.bean.*;
import club.justwill.single.bean.Stick.DiscussProject;
import club.justwill.single.bean.Stick.ReplyDiscuss;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.persistence.document.ProjectHits;
import club.justwill.single.persistence.document.ProjectSupport;
import club.justwill.single.persistence.document.Stick;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

/**
 * Created by xiangnan on 15-12-26.
 */
@RestController
@Scope(WebApplicationContext.SCOPE_SESSION)
public class ProjectsController extends BaseController implements Serializable {

    @Autowired
    private ProjectsService projectsService;

    @Autowired
    private ProjectAssemble pAssemble;

    @Autowired
    private StickAssemble stickAssemble;

    @Autowired
    private ProjectSupportAssemble projectSupportAssemble;

    @RequestMapping(value = URL_PROJECT_SEARCH, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchProjects(@RequestBody SearchProjects searchProjects) {
        JsonResponse obj = new JsonResponse();
        List<Projects> projectsList = projectsService.searchProjects(searchProjects, obj);
        obj.setData(pAssemble.assembleProjectMiniLisit(projectsList));
        obj.setSuccessful();
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_CREATE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse createProject(@RequestBody CreateProject createProject) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Projects projects = projectsService.addProjects(token, createProject, obj);

        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            if(projects!=null && projects.getProjectId()!=null && projects.getProjectId() != 0){
                ProjectsResp pResp = pAssemble.assembleProject(projects);
                obj.setData(pResp);
                obj.setSuccessful();
            }
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_UPDATE, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse updateProject(@PathVariable long projectId,@RequestBody CreateProject updateProject) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        projectsService.updateProject(token, updateProject, projectId,obj);

        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ADD_AGREEMENT, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse addAgreement(@RequestBody AddAgreement addAgreement) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        projectsService.addAggrement(token, addAgreement, obj);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_ADD_SKU, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse addSkus(@RequestBody AddSKu addSKu) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Long skuId = projectsService.addSKus(token, addSKu, obj);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            SkusResp sku = new SkusResp();
            sku.setSkuId(String.valueOf(skuId));
            obj.setData(sku);
            obj.setSuccessful();
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_UPDATE_SKU, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse addSkus(@RequestBody UpdateSku sku) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        projectsService.updateSKus(token, sku, obj);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

    @RequestMapping(value = URL_PROJECT_SUPPORT_INFO, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchSupportInfo(@PathVariable long projectId) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        ProjectSupport projectSupport = projectsService.searchSupportInfo(projectId,obj);

        if(JsonResponseHelper.isHasError(obj)){
            obj.setFailed();
        } else {
            obj.setData(projectSupportAssemble.assembleProjectSupport(projectSupport));

            obj.setSuccessful();
        }
        return obj;
    }

    @RequestMapping(value = URL_PROJECT_COMMENTS_SEARCH, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse searchComments(@PathVariable Long projectId) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        List<Stick> stickList = projectsService.searchProjectStick(projectId);
        obj.setData(stickAssemble.assembleStickList(stickList));
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_DISCUSS, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse discuss(@RequestBody DiscussProject discussProject) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Stick stick = projectsService.discuss(token, obj, discussProject);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
            obj.setData(stickAssemble.assembleStick(stick));
        }
        return obj;
    }

    @LoginRequired
    @RequestMapping(value = URL_PROJECT_REPLY, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse replyDiscuss(@RequestBody ReplyDiscuss replyDiscuss) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        Stick stick = projectsService.replyDiscuss(token, obj, replyDiscuss);
        if (JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
            obj.setData(stickAssemble.assembleStick(stick));
        }
        return obj;
    }


    @RequestMapping(value = URL_PROJECT_GET, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse getProjectById(@PathVariable long projectId, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        ProjectsResp projectsResp = projectsService.getProjectById(token,projectId,obj);
        if(JsonResponseHelper.isHasError(obj)){
            obj.setFailed();
        } else {
            obj.setData(projectsResp);
            obj.setSuccessful();
        }
        return obj;
    }

    @RequestMapping(value = URL_PROJECT_GET_BY_STATUS_TAG, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse getProjectByTagAndStatus(@RequestBody GetProjectByTS getProjectByTS) {
        JsonResponse obj = new JsonResponse();
        SecurityToken token = new SecurityToken(sessionState.getUserId());
        token.generateToken(sessionState);
        List<ProjectsResp> projectsRespList = projectsService.getProjectByTagAndStatus(token,obj,getProjectByTS);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(projectsRespList);
            obj.setSuccessful();
        }
        return obj;

    }

    @RequestMapping(value = URL_PROJECT_GET_PROJECT_HITS, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse getProjectHits(@PathVariable Long projectId) {
        JsonResponse obj = new JsonResponse();
        ProjectHits projectHits = projectsService.getProjectHits(obj, projectId);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setData(new ProjectHitsResp(projectHits));
            obj.setSuccessful();
        }
        return obj;
    }

    @RequestMapping(value = URL_PROJECT_AA_PROJECT_HITS, method = RequestMethod.POST, produces = CoreConstants.MIME_JSON)
    public JsonResponse addProjectHits(@PathVariable Long projectId) {
        JsonResponse obj = new JsonResponse();
        projectsService.addProjectsHits(obj, projectId);
        if(JsonResponseHelper.isHasError(obj)) {
            obj.setFailed();
        } else {
            obj.setSuccessful();
        }
        return obj;
    }

}
